import 'package:flutter/material.dart';

import 'plate_styles.dart';

/// 键盘控制器
class PayKeyboardController {
  PayKeyboardController();

  /// 键盘悬浮窗口
  OverlayEntry? _keyboardOverlay;

  /// 键盘进入和退出动画控制器
  AnimationController? _controller;

  /// 键盘可见状态
  bool _isKeyboardShowing = false;

  /// 主题
  PlateStyles? _styles;
  Function(int index, String value)? _onPlateNumberChanged;

  set animationController(AnimationController controller) =>
      _controller = controller;

  set styles(PlateStyles styles) => _styles = styles;

  set onPlateNumberChanged(onPlateNumberChanged(int index, String value)) =>
      _onPlateNumberChanged = onPlateNumberChanged;

  /// 显示键盘
  void showKeyboard(BuildContext context,{Widget? child}) {
    if (_keyboardOverlay != null) {
      _keyboardOverlay?.remove();
    }
    _keyboardOverlay = OverlayEntry(
      builder: (context) {
        return child ?? Container();
      },
    );
    Overlay.of(context)?.insert(_keyboardOverlay!);
    if (!_isKeyboardShowing) {
      _controller?.forward();
      _isKeyboardShowing = true;
    }
  }

  /// 隐藏键盘
  void hideKeyboard() {
    if (isKeyboardShowing()) {
      _controller?.reverse();
    }
    _isKeyboardShowing = false;
  }

  /// 键盘是否可见
  bool isKeyboardShowing() {
    return _keyboardOverlay != null && _isKeyboardShowing;
  }

  /// 移除悬浮窗口
  void dispose() {
    if (_keyboardOverlay != null) {
      _keyboardOverlay?.remove();
      _keyboardOverlay = null;
    }
  }
}